package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IBmClassTypeDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.Tool;
import com.wsj.web.vo.homeSite.BmClassType;
import com.wsj.web.vo.homeSite.BmNewsType;

public class BmClassTypeDaoImpl extends HibernateDaoSupport implements IBmClassTypeDao {

	public void add(BmClassType bmClassType) throws DataAccessException {
		// TODO Auto-generated method stub
		
		this.getHibernateTemplate().save(bmClassType);

	}

	public void delete(Integer id) throws DataAccessException {
		// TODO Auto-generated method stub
		BmClassType bmClassType=(BmClassType) this.getHibernateTemplate().get(BmClassType.class, id);
		this.getHibernateTemplate().delete(bmClassType);

	}

	public void update(BmClassType bmClassType) throws DataAccessException {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(bmClassType);

	}

	public BmClassType getBmClassTypeByClassTypeName(String classTypeName) throws DataAccessException {
		// TODO Auto-generated method stub
		List list=new ArrayList();
		BmClassType bmClassType=null;
		String sql = "from BmClassType as bm where bm.classTypeName= ? ";
		list=this.getHibernateTemplate().find(sql,classTypeName);
		
		if(list!=null&&!list.isEmpty()){
			bmClassType=(BmClassType) list.get(0);
			
		}
		
		return bmClassType;
	}

	public BmClassType getBmClassTypeById(Integer id) throws DataAccessException {
		// TODO Auto-generated method stub
		
		BmClassType bmClassType=(BmClassType) this.getHibernateTemplate().get(BmClassType.class, id);
		return bmClassType;
	}

	public PageResultSet loadBmClassTypeByParam(String classTypeName, int page, int pageSize) throws DataAccessException {
		// TODO Auto-generated method stub
//		 初始化
		PageResultSet pageResultSet = new PageResultSet();

		PerfectPageResultSet pageResult = new PerfectPageResultSet();

		// 获取Session
		Session session = SessionFactoryUtils.getSession(getSessionFactory(),
				true);

		// 初始化SQL语句
		StringBuffer stringBuffer = new StringBuffer("from BmClassType as bm where 1=1 ");

		// 设置每页显示得记录数和开始页
		pageResult.setPage(page, pageSize);
		
        // 参数
        List<Comparable> list = new ArrayList<Comparable>();
        if(classTypeName!= null&&!classTypeName.equals("")){
            stringBuffer.append(" and bm.classTypeName like ?");
            
            list.add("%"+classTypeName+"%");
        }
        
		stringBuffer.append(" order by bm.id desc ");
		
		 String[] strParams = Tool.toArray(list);
	        pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),
	                session, strParams);
		
		
		return pageResultSet;
	}

	public List loadAllBmClassType() throws DataAccessException {
		// TODO Auto-generated method stub
		List list =new ArrayList();
		list=this.getHibernateTemplate().find("from BmClassType as bm where 1=1");
		return list;
	}

	/****
	 *  通过资讯分类名称,获取资讯类型对象
	 */
	public BmClassType loadBmClassTypeByNameDao(String name) throws DataAccessException 
	{
	     String sql="from BmClassType as c where c.classTypeName = ?";
	     List li=this.getHibernateTemplate().find(sql,name);
	     if(li!=null && !li.isEmpty())
	     {
	    	 return (BmClassType)li.get(0);
	     }
	     
	     return null;
	}

}




















